package main

import (
	"fmt"
	"reflect"
)

type People struct {
	Name   string `json:"name" from:"test"`
	Age    int    `json:"age"`
	Gender string `json:"gender"`
}

// 打印结构体字段
func printStructField(p interface{}) {
	t := reflect.TypeOf(p)
	if t.Kind() != reflect.Struct && t.Elem().Kind() != reflect.Struct {
		fmt.Println("传入的参数不是结构体")
		return
	}

	//1、通过类型变量里面的Field可以获取结构体的字段
	field0 := t.Field(0)
	fmt.Printf("字段名：%v\n", field0.Name)               //字段名：Name
	fmt.Printf("字段类型：%v\n", field0.Type)              //段类型：string
	fmt.Printf("字段映射字段：%v\n", field0.Tag.Get("json")) //字段映射字段：name
	fmt.Printf("字段映射字段：%v\n", field0.Tag.Get("from")) //字段映射字段：test
	// fmt.Printf("%#v\n", field0)
}

func main() {
	var p = People{Name: "张三", Age: 18, Gender: "男"}

	printStructField(p) //{Name  string json : "name" 0 [0] false}
}
